package com.changli04._set;

import org.junit.Test;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Objects;
import java.util.TreeSet;

public class treeSetTest {
    @Test
    public void test() {
        TreeSet ts = new TreeSet();
        ts.add("DD");
        ts.add("KK");
        ts.add("PP");
        ts.add("MM");
        ts.add("LL");
        ts.add("GG");
//        ts.add(123);//ClassCastException
        Iterator it = ts.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }


    }
    @Test
    public void test2() {
        TreeSet ts = new TreeSet();
        User u1 = new User("Jack",23);
        User u2 = new User("Davids",43);
        User u3 = new User("Candy",23);
        User u4 = new User("Lucy",56);
        User u5 = new User("Jackson",11);
        ts.add(u1);
        ts.add(u2);
        ts.add(u3);
        ts.add(u4);
        ts.add(u5);
        Iterator it = ts.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }


    }


    @Test
    public void test3() {
        Comparator comp = new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof User && o2 instanceof User) {
                    User u1 = (User) o1;
                    User u2 = (User) o2;
                    int value = u1.getAge() - u2.getAge();
                    if (value != 0){
                        return value;
                    }
                    else {
                        return u1.getName().compareTo(u2.getName());
                    }

                }
                throw new RuntimeException("输入类型不匹配");
            }
        };

        TreeSet ts = new TreeSet(comp);
        User u1 = new User("Jack",23);
        User u2 = new User("Davids",43);
        User u3 = new User("Candy",23);
        User u4 = new User("Lucy",56);
        User u5 = new User("Jackson",11);
        ts.add(u1);
        ts.add(u2);
        ts.add(u3);
        ts.add(u4);
        ts.add(u5);
        Iterator it = ts.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }



    }


}
class User implements Comparable {
    String name;
    int age;
    public User(String name, int age) {
        this.name = name;
        this.age = age;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return age == user.age && Objects.equals(name, user.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public int compareTo(Object o) {
        if(this == o) return 0;
        if(o instanceof User){
            User u = (User) o;
            int value =  this.age - u.age;
            if(value != 0) return value;
            return this.name.compareTo(u.name);
        }
        throw new RuntimeException("输入类型不匹配！！");
    }
}
